<template>
  <div class="Menu">
    <el-menu
      :class="{
        'el-menu-vertical': mode === 'vertical',
      }"
      :mode="mode"
      :collapse="isCollapse"
      router
      :default-active="'/' + route.meta.belongMenu"
      :unique-opened="true"
      :background-color="background"
      :text-color="text"
      :active-text-color="activeText">
      <div
        v-for="(level_1, level_1_index) in data"
        :key="level_1_index"
        class="menu-item noSelect">
        <!-- level 1 -->
        <el-menu-item v-if="level_1.path" :index="level_1.path">
          <i :class="level_1.icon" class="iconfont iconfont-m"></i>
          <template #title>
            <span>{{ level_1.title }}</span>
          </template>
        </el-menu-item>

        <!-- level 1 -->
        <el-sub-menu v-if="!level_1.path" :index="String(level_1_index)">
          <!-- level 1 -->
          <i :class="level_1.submenu!.icon" class="iconfont iconfont-m"></i>
          <template #title>
            <span v-show="!isCollapse">
              {{ level_1.submenu!.submenuTitle }}
            </span>
          </template>

          <!-- level 2 -->
          <div
            v-for="(level_2, leve_2_Index) in level_1.submenu!.submenuItems"
            :key="leve_2_Index">
            <!-- level 2 -->
            <el-menu-item v-if="level_2.path" :index="level_2.path">
              <i :class="level_2.icon" class="iconfont iconfont-m"></i>
              <template #title>
                <span>{{ level_2.title }}</span>
              </template>
            </el-menu-item>

            <!-- level 2 -->
            <el-sub-menu index="submenuIndex" v-if="!level_2.path">
              <!-- level 3 -->
              <i :class="level_2.submenu!.icon" class="iconfont iconfont-m"></i>
              <template #title>
                <span>{{ level_2.submenu!.submenuTitle }}</span>
              </template>

              <!-- level 3 -->
              <el-menu-item
                v-for="(level_3, level_3_index) in level_2.submenu!.submenuItems"
                :key="level_3_index"
                :index="level_3.path">
                <i :class="level_3.icon" class="iconfont iconfont-m"></i>
                <template #title>
                  <span>{{ level_3.title }}</span>
                </template>
              </el-menu-item>
            </el-sub-menu>
          </div>
        </el-sub-menu>
      </div>

      <!-- 展开和关闭 -->
      <!-- <div class="collapse" @click="handleCollapse" v-if="isHasCollapseBtn">
        <el-icon :size="20" v-if="!isCollapse">
          <Back />
        </el-icon>
        <el-icon :size="20" v-else>
          <Right />
        </el-icon>
      </div> -->
    </el-menu>
  </div>
</template>

<script lang="ts" setup>
import type {IMenuData } from '../../config/project-base/menu'
import { useRoute } from "vue-router";
// import { Back, Right } from "@element-plus/icons-vue";

// 菜单props的类型  注意这个是不能外部导入 否则会出错
interface IMenuProps {
  data: IMenuData[];
  mode?: string;
  isCollapse?: boolean;
  background?: string;
  text?: string;
  activeText?: string;
  isHasCollapseBtn?: boolean;
}

// emit接受的类型
// interface IEmit {
//   (e: "handleCollapse"): void;
// }

// 设置props的默认值
withDefaults(defineProps<IMenuProps>(), {
  mode: "vertical",
  isCollapse: false,
  background: "#fff",
  text: "#000000",
  activeText: "rgb(80, 122, 210)",
  data: () => [],
  isHasCollapseBtn: true,
});

const route = useRoute();
// const emit = defineEmits<IEmit>();

// const handleCollapse = () => {
//   emit("handleCollapse");
// };
</script>

<style scoped lang="scss">
.Menu {
  position: relative;
  height: 100%;
  .el-menu {
    border: none;
    position: relative;
  }

  .collapse {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 50px;
    cursor: pointer;
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    border-top: 1px solid var(--primary-border);

    /* background-color: #1b4caf; */
    i {
      /* color: #fff; */
      font-size: 20px;
      font-weight: bold;
    }
  }

  .iconfont-m {
    margin-right: 8px;
    font-size: 17px;
  }

  .el-menu-vertical:not(.el-menu--collapse) {
    width: 220px;
  }

  :deep(.el-submenu__icon-arrow) {
    font-size: 17px;
  }
  :deep(.el-menu) {
    height: 100%;
  }
  :deep(.el-icon-arrow-right) {
    display: none;
  }
  :deep(.el-menu-item:hover) {
    background-color: rgb(81, 85, 90);
  }
  :deep(.el-menu-item.is-active){
    background-color:rgb(81, 85, 90);
    color: #fff;
  }
  :deep(.el-menu-item){
    font-size: 15px;
    display: flex;
    align-items: center;
  }
}
</style>
